package com.core.family.admin.service.Impl;

import com.core.family.admin.service.*;
import com.core.common.gen.BaseServiceImpl;
import com.core.family.admin.domain.dto.RoleGp;
import com.core.family.admin.domain.dto.UserGp;
import com.core.family.admin.domain.po.SysOrg;
import com.core.family.admin.domain.po.SysUser;
import com.core.family.admin.mapper.SysOrgMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author honglei
 * @version 1.0
 * @time: 2022-6-10 11:46:34
 * @Description 服务实现层
 */
@Slf4j
@Service
public class SysOrgServiceImpl extends BaseServiceImpl<SysOrgMapper, SysOrg> implements SysOrgService {

    @Autowired
    SysMenuService menuService;
    @Autowired
    SysRoleGService roleGService;
    @Autowired
    SysRoleMenuService roleMenuService;

    @Autowired
    SysUserGroupService userGroupService;

    @Autowired
    SysUserService userService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean register(SysOrg bean) {
        //直接插 组织，重复限制 使用数据库约束 抛出异常统一处理
        this.baseMapper.insert(bean);

        SysUser user = new SysUser();
        user.setOrgId(bean.getId());
        user.setMobile(bean.getMobile());
        user.setPassword("111111");

        //直接插 管理员，重复限制 使用数据库约束 抛出异常统一处理
        userService.save(user);


        //添加用户到用户组
        UserGp group = new UserGp();
        group.setOrgId(bean.getId());
        group.setUid(user.getId());
        group.setGname("系统管理");
        group.setGCode("gp-admin");
        userGroupService.saveUserGp(group);

        //添加组权限  系统管理员

        RoleGp role = new RoleGp();
        role.setOrgId(bean.getId());
        role.setGid(group.getGid());
        role.setRoleCode("role-admin");
        role.setRname("系统管理员");
        roleGService.saveRoleGp(role);

        roleMenuService.bangAllMenus(bean.getId(), role.getRid());


        return true;
    }

}

